Skip to content

Server rendering and hydration (#66, Part 7) - #75

Merged
n4ff4h merged 7 commits into
devfrom
fix/66-part7-ssr
Nov 18, 2026
Merged

n4ff4h merged 7 commits into
devfrom
fix/66-part7-ssr

Conversation

@n4ff4h

@n4ff4h n4ff4h commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Fixes Part 7 of #66: server rendering and hydration. Every item has a test that failed first. 7.1 to 7.5 use a new test/hydrate.tsx helper (renderToString, then browser-only setup, then hydrateRoot). 7.6 and 7.7 use the e2e/nextjs fixture.

Item Change
7.1 ThemeProvider reads the stored theme with useSyncExternalStore (server snapshot null, so it hydrates with defaultTheme and then switches). It also follows the storage event (other tabs). If storage is blocked, a choice is kept in memory for the visit. ThemeToggle variant="segmented" now selects the stored theme after hydration. ui/sonner.tsx reads useTheme(), so this fixes it too.
7.2 themeScript takes the provider's options as an object, themeScript({ defaultTheme, forcedTheme, storageKey }). It applies forcedTheme, and a stored value that isn't a theme falls back to defaultTheme, like the provider does.
7.3 ShareButtons without url reads the page URL with useSyncExternalStore (server snapshot ""), so the links get the URL after hydration instead of keeping stale hrefs.
7.4 FullCalendar no longer seeds state from useIsMobile(). Until a view is picked, the view is derived (view ?? picked ?? defaultView ?? (isMobile ? "agenda" : "month")), so phones switch to the agenda after hydration.
7.5 New internal useToday(pinned?) hook: null while server rendering and hydrating, the device's date after that. DateCalendar and HijriCalendar mark no day as today until it is known. HijriCalendar's roving focus target is now [focused, selected, today] with a fallback to the 1st.
7.6 The build adds "use client" only to modules whose source starts with the directive (a small Vite plugin plus a banner function). It is now on 286 of 396 output modules. themeScript moved to providers/theme-script.ts. 13 modules that use hooks or context and had relied on the blanket banner now have the directive in source (icons/icon*, hooks/*, charts/format, files/file-utils, form/required-marker, layout/feature-grid, and the ui/breadcrumb, ui/pagination and ui/native-select primitives, which have // onu: notes). New src/use-client.test.ts keeps it that way. The fixture's root layout calls themeScript() and its page calls cn, transliterate (/thaana) and toCsv (/export), all in Server Components.
7.7 New e2e/nextjs/app/hydration page (segmented ThemeToggle, ShareButtons without url, FullCalendar). ssr-check.mjs opens it at 375px with onu-theme=dark stored and checks the dark class, the selected toggle, the share href and data-view="agenda", in both locales.

Skipped ([P] / device check)

  • None. Part 7 has no [P] items.

Could not reproduce

  • None. Every item reproduced with a failing test.

Behaviour and API changes

  • themeScript(storageKey, defaultTheme) is now themeScript({ storageKey, defaultTheme, forcedTheme }).
  • Theme, ResolvedTheme and the new ThemeScriptOptions live in providers/theme-script.ts. They are still exported from the root, and ThemeProviderProps extends ThemeScriptOptions.
  • ThemeProvider follows theme changes made in other tabs.
  • An uncontrolled FullCalendar with no defaultView follows the screen width (agenda below md) until the user picks a view.
  • When server-rendered, calendars without today mark no day as today until hydration. Pass today to have it in the server HTML.
  • Known limitation (7.5): with no selection, defaultMonth or today, the initial month still comes from the client clock while hydrating. On the 1st of a month between 00:00 and 05:00 MVT, a UTC server's month can differ. That is a text mismatch, which React recovers from by client-rendering. Pass defaultMonth or today to avoid it.
  • Docs: guides/dark-mode.md, guides/inertia-and-nextjs.md (the ThemeScript client wrapper is gone), share-buttons.md, full-calendar.md, date-calendar.md, hijri-date-picker.md, and CONTRIBUTING.md (a rule for adding "use client").

Verification

  • pnpm lint && pnpm format:check && pnpm typecheck: pass
  • pnpm --filter @javaabu/onu exec vitest run --maxWorkers=3: 198 files, 2869 passed, 1 skipped
  • node scripts/check-docs.mjs: Docs OK. node scripts/boost.mjs --check: OK
  • pnpm build && pnpm --filter @javaabu/onu size: pass, no budget changes
  • pnpm --filter onu-nextjs-fixture build && pnpm --filter onu-nextjs-fixture test:ssr: both locales pass (11 HTML strings, clean hydration, browser-only state applied)
  • Before 7.6, the SSR check failed with Attempted to call cn() from the server but cn is on the client (and the same for themeScript).
  • To check that 7.7 catches regressions, I put back the old ShareButtons, the old FullCalendar and a ThemeProvider that reads storage while hydrating. test:ssr then reported all three problems in both locales.

🤖 Generated with Claude Code

agent and others added 7 commits November 18, 2026 05:33
…66, 7.1)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eme (#66, 7.2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n the Next.js fixture (#66, 7.7)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@n4ff4h
n4ff4h merged commit baeae7d into dev Nov 18, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant